//
//  NewsTableViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-6-4.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "NewsTableViewController.h"
#import "UIImageView+AFNetworking.h"
#import "CustomTableViewCell.h"
#import "NewsDetailViewController.h"
#import "MBProgressHUD.h"



#define HEIGHT (([[UIDevice currentDevice].systemVersion floatValue] < 7.0 )?44.0:64.4)
@interface NewsTableViewController ()



@end

@implementation NewsTableViewController
@synthesize  dataArray=_dataArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.frame = [[UIScreen mainScreen] bounds];
        self.view.backgroundColor = [UIColor whiteColor];
        
        _tableView = [[PullTableView alloc] initWithFrame:CGRectMake(0, HEIGHT, 320, self.view.frame.size.height-self.navBar.frame.size.height)];
        
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        
        _tableView.delegate = self;
        _tableView.dataSource= self;
        [self.view addSubview:_tableView];
        
        
        _tableView.pullArrowImage = [UIImage imageNamed:@"blackArrow"];
        _tableView.pullBackgroundColor = [UIColor whiteColor];
        _tableView.pullTextColor = [UIColor blackColor];
        _dataArray = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    [self reloadAllView];
}

// 子类中重写该方法
- (void)reloadAllView
{
    [_tableView reloadData];
    [self.view setNeedsDisplay];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



#pragma mark tableView datasoure

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseIdetify = @"SvTableViewCell";
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdetify];
    if (!cell) {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdetify];
        
    }
    if ([_dataArray count] !=0) {
        cell.titleLable.text = [[_dataArray objectAtIndex:indexPath.row] objectForKey:@"title"];
        cell.hits.text = [NSString stringWithFormat:@"%@",[[_dataArray objectAtIndex:indexPath.row] objectForKey:@"hits"]];
        cell.tag = [[[_dataArray objectAtIndex:indexPath.row] objectForKey:@"id"] intValue];
        
       
      
        NSString *description = [[_dataArray objectAtIndex:indexPath.row] objectForKey:@"description"];
        
        description = [description stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
        description = [description stringByReplacingOccurrencesOfString:@"&rdquo;" withString:@""];
        description = [description stringByReplacingOccurrencesOfString:@"&ldquo;" withString:@""];
        description = [description  stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

        cell.description.text = description;
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"URL" ofType:@"plist"];
        NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
        
        NSString *imageString = [NSString stringWithFormat:@"http://%@%@",[data objectForKey:@"IPAndPort"],[[_dataArray objectAtIndex:indexPath.row] objectForKey:@"image"]];
       
        [cell.samllImageView setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"base_detail_default_pic.png"]];
        
    }
    return cell;
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return [_dataArray count];
    
}

#pragma mark tableView delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSNumber* ef = [[_dataArray objectAtIndex:indexPath.row] objectForKey:@"id"];
    NewsDetailViewController* viw = [[NewsDetailViewController alloc] initWithNewsId:ef];
    
    [self.navigationController pushViewController:viw animated:YES];
    
}

#pragma mark 提示信息
- (void)showMessage:(NSString *)message
{
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    HUD.labelText = message;
    [self.view addSubview:HUD];
    [HUD showAnimated:YES
  whileExecutingBlock:^{
      sleep(1);
  }
      completionBlock:^{
          [HUD removeFromSuperview];
      }];
    
}

@end
